/*
 * Copyright (c) [2025-07-21] [lihainuo]
 *
 * Licensed under the MIT License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
package com.lihainuo.springai.redis.strater;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis Chat Memory 自动配置类
 */
@AutoConfiguration(after = RedisAutoConfiguration.class)
@ConditionalOnClass({RedisTemplate.class, ChatMemory.class})
@ConditionalOnProperty(prefix = "spring.ai.chat.memory.redis", name = "enabled", havingValue = "true", matchIfMissing = true)
public class RedisChatMemoryAutoConfiguration {

    /**
     * 配置专门用于 Spring AI 的 RedisTemplate
     */
    @Bean("springAiRedisTemplate")
    @ConditionalOnMissingBean(name = "springAiRedisTemplate")
    public RedisTemplate<String, SerializableMessage> springAiRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, SerializableMessage> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 配置支持多态的 Jackson 序列化器
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.activateDefaultTyping(
                objectMapper.getPolymorphicTypeValidator(),
                ObjectMapper.DefaultTyping.EVERYTHING,
                JsonTypeInfo.As.PROPERTY
        );

        GenericJackson2JsonRedisSerializer serializer =
                new GenericJackson2JsonRedisSerializer(objectMapper);

        // 配置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

    /**
     * 配置 RedisChatMemory Bean
     */
    @Bean
    @ConditionalOnMissingBean(ChatMemory.class)
    public ChatMemory redisChatMemory(
            RedisTemplate<String, SerializableMessage> redisTemplate) {

        return new RedisChatMemory(redisTemplate);
    }
}
